package com.example.hans.retrofitrxjavaokhttp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class MainActivity extends AppCompatActivity {


    public static final String BASE_URL = "https://api.douban.com/v2/movie/";

//    public List<Movie> mMovieList = new ArrayList<>();

    private RecyclerView mRecyclerView;
    private MovieAdapter mMovieAdapter = new MovieAdapter();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);







        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //获取接口实例
        MovieService movieService = retrofit.create(MovieService.class);

        //调用方法得到一个Call
        Call<MovieSubject> call = movieService.getTop250(0,20);

        //进行网络请求
        call.enqueue(new Callback<MovieSubject>() {

            @Override
            public void onResponse(Call<MovieSubject> call, Response<MovieSubject> response) {
                mMovieAdapter.setMovieList(response.body().subjects);
                mMovieAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<MovieSubject> call, Throwable t) {
                t.printStackTrace();
            }

        });



        mRecyclerView = (RecyclerView)findViewById(R.id.recycler_movie_main);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mMovieAdapter);
    }


    public interface MovieService {

        //获取豆瓣top250榜单
        @GET("top250")
        Call<MovieSubject> getTop250(@Query("start")int start,@Query("count")int count);

    }

    public class MovieSubject {
        public int count;
        public int start;
        public int total;
        public List<Movie>  subjects;
        public String title;

    }
}
